package org.qiaice.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.qiaice.context.BaseContext;
import org.qiaice.entity.Employee;
import org.qiaice.entity.dto.EmployeeDTO;
import org.qiaice.entity.vo.EmployeeLoginVO;
import org.qiaice.exception.BaseException;
import org.qiaice.mapper.EmployeeMapper;
import org.qiaice.result.PageResult;
import org.qiaice.service.EmployeeService;
import org.qiaice.util.TokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Objects;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Override
    public EmployeeLoginVO login(String username, String password) {
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        Employee employee = lambdaQuery()
                .eq(Employee::getUsername, username)
                .eq(Employee::getPassword, password)
                .one();
        if (Objects.isNull(employee)) {
            throw new BaseException("用户名或密码错误");
        }
        if (employee.getStatus() == 0) {
            throw new BaseException("用户被封禁");
        }
        return EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(username)
                .name(employee.getName())
                .token(TokenUtil.createToken(employee, false))
                .build();
    }

    @Override
    public PageResult<Employee> pageQuery(String name, Integer page, Integer pageSize) {
        Page<Employee> paged = lambdaQuery()
                .like(StringUtils.hasText(name), Employee::getName, name)
                .orderByAsc(Employee::getId)
                .page(Page.of(page, pageSize));
        return new PageResult<>(paged.getTotal(), paged.getRecords());
    }

    @Override
    public void changeStatus(Integer status, Long id) {
        updateById(Employee.builder().id(id).status(status).build());
    }

    @Override
    public void saveEmployee(EmployeeDTO employeeDTO) {
        Employee employee = Employee.builder().build();
        BeanUtils.copyProperties(employeeDTO, employee);
        save(employee);
    }

    @Override
    public void changePassword(Long empId, String oldPassword, String newPassword) {
        empId = BaseContext.getContext().get("id", Long.class);
        Employee employee = lambdaQuery().eq(Employee::getId, empId).one();
        if (Objects.isNull(employee)) {
            throw new BaseException("id 为 " + empId + " 的员工不存在");
        }
        oldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes(StandardCharsets.UTF_8));
        if (!employee.getPassword().equals(oldPassword)) {
            throw new BaseException("原始密码错误");
        }
        newPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes(StandardCharsets.UTF_8));
        updateById(Employee.builder().id(empId).password(newPassword).build());
    }

    @Override
    public void change(EmployeeDTO employeeDTO) {
        Long id = employeeDTO.getId();
        if (Objects.isNull(lambdaQuery().eq(Employee::getId, id).one())) {
            throw new BaseException("id 为 " + id + " 的员工不存在");
        }
        Employee employee = Employee.builder().build();
        BeanUtils.copyProperties(employeeDTO, employee);
        updateById(employee);
    }
}
